﻿using System;
using System.Linq;
using System.Collections.Generic;
using NHibernate.Cfg;
using NHibernate;


namespace epicenter2010.NH.Caching
{
    /// <summary>
    /// Repository
    /// bstack @ 08/09/2009
    /// </summary>
    public class Repository
    {
        private readonly ISessionFactory c_sessionFactory;
        private ISession c_session;


        /// <summary>
        /// Ctor
        /// </summary>
        public Repository()
        {
            this.c_sessionFactory = (new Configuration()).Configure().BuildSessionFactory();
        }

        /// <summary>
        /// Strat a session
        /// </summary>
        public void StartSession()
        {
            this.c_session = this.c_sessionFactory.OpenSession();
        }


        /// <summary>
        /// End session
        /// </summary>
        public void EndSession()
        {
            this.c_session.Close();
        }


        /// <summary>
        /// Get by Id
        /// </summary>
        /// <param name="id">Id</param>
        public epicenter2010.NH.Caching.Customer GetById(
            int id)
        {
            return this.c_session.Get<epicenter2010.NH.Caching.Customer>(id);
        }


        /// <summary>
        /// Get by Id using load
        /// </summary>
        /// <param name="id">Id</param>
        public epicenter2010.NH.Caching.Customer GetByIdUsingLoad(
            int id)
        {
            return this.c_session.Load<epicenter2010.NH.Caching.Customer>(id);
        }


        /// <summary>
        /// Get by Id
        /// </summary>
        /// <param name="id">Id</param>
        public void Save(
            epicenter2010.NH.Caching.Customer subject)
        {
            this.c_session.SaveOrUpdate(subject);
            this.c_session.Flush();
        }
    }
}
